unit uFrmSetRake;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, UUTILS, umainutils, Keyboard, strutils, Buttons;

type
  TFrmSetRake = class(TForm)
    edt_Bet: TLabeledEdit;
    edt_RakeValue: TLabeledEdit;
    TouchKeyboard1: TTouchKeyboard;
    Panel1: TPanel;
    pnl_Buttons: TPanel;
    btn_Save: TSpeedButton;
    Panel2: TPanel;
    btn_CE: TButton;
    btn_Backspace: TButton;
    procedure edt_BetChange(Sender: TObject);
    procedure btn_CancelClick(Sender: TObject);
    procedure btn_OKClick(Sender: TObject);
    procedure btn_CancelKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure edt_RakeValueChange(Sender: TObject);
    procedure btn_CEClick(Sender: TObject);
    procedure btn_BackspaceClick(Sender: TObject);
  private
    { Private declarations }
    m_CashGameID, m_TableID, m_RakeId: string;
    function GetRake(PotValue: string): string;
  public
    { Public declarations }
    procedure ShowSetRakeForm(CashGameID, TableID: string);
  end;

var
  FrmSetRake: TFrmSetRake;

implementation

uses uFrmMainLPT;
{$R *.dfm}

procedure TFrmSetRake.btn_BackspaceClick(Sender: TObject);
begin
  edt_Bet.Text := MidStr(edt_Bet.Text, 0, Length(edt_Bet.Text) - 1);
  if edt_Bet.Text = '' then
    edt_Bet.SetFocus;
end;

procedure TFrmSetRake.btn_CancelClick(Sender: TObject);
begin
  close();
end;

procedure TFrmSetRake.btn_CancelKeyPress(Sender: TObject; var Key: Char);
begin
  // The Escape key is #27.
  if Key = #27 then
    close;
end;

procedure TFrmSetRake.btn_CEClick(Sender: TObject);
begin
  edt_Bet.Text := '';
  edt_Bet.SetFocus;
end;

procedure TFrmSetRake.btn_OKClick(Sender: TObject);
begin
  InsertDataIntoDB('CASH_GAME_RAKES', 'CASH_GAME_ID,TABLE_ID,RAKE_VALUE,DATETIME', m_CashGameID + ',' + m_TableID + ',' + '''' + edt_RakeValue.Text + '''' + ',' + 'to_date(''' +
      GetFormatedStringDateNOW(OraSessMainUtils) + ''', ''dd.mm.yyyy hh24:mi:ss'')');
  close();
end;

procedure TFrmSetRake.edt_BetChange(Sender: TObject);
var
  RakeValue: string;
begin
  if not IsStrANumber(edt_RakeValue.Text) then
  begin
    edt_RakeValue.Text := LeftStr(edt_RakeValue.Text, Length(edt_RakeValue.Text) - 1);
  end;
  // if edt_Bet.Text <> '' then
  begin
    edt_RakeValue.Text := GetRake(edt_Bet.Text);
  end;
end;

procedure TFrmSetRake.edt_RakeValueChange(Sender: TObject);
var
  RakePerc, BetVal: double;
begin
  // edt_RakeValue.Text := FloatToStr(GetRake(edt_Bet.Text));
  // edt_Bet.Text := FloatToStr(BetVal / (RakePerc / 100));
end;

procedure TFrmSetRake.FormShow(Sender: TObject);
begin
  edt_Bet.SetFocus;
  edt_Bet.Text := '';
  edt_RakeValue.Text := '';
end;

function TFrmSetRake.GetRake(PotValue: string): string;
begin
  OraQueryMainUtils.close;
  OraQueryMainUtils.SQL.Clear;
  OraQueryMainUtils.SQL.Text := ('select case when is_percent=1 then (' + '''' + PotValue + '''' + '*value/100) else (value) end as rake_value from rake_values where rake_id=' + m_RakeId + ' and ' +
      '''' + PotValue + '''' + ' between from_value and to_value');
  OraQueryMainUtils.Execute;
  result := '';
  if not OraQueryMainUtils.Eof then
  begin
    result := VarToStr(OraQueryMainUtils.Field(0));
  end;
end;

procedure TFrmSetRake.ShowSetRakeForm(CashGameID, TableID: string);
begin
  OraQueryMainUtils.close;
  OraQueryMainUtils.SQL.Clear;
  OraQueryMainUtils.SQL.Text := ('select rake_id from cash_games where id=' + CashGameID);
  OraQueryMainUtils.Execute;
  m_RakeId := OraQueryMainUtils.Field(0);
  m_CashGameID := CashGameID;
  m_TableID := TableID;
  ShowModal;
end;

end.
